import os
import cv2
import pyautogui
from ultralytics import YOLO
import time
from moveFb模块 import movefb
import win32gui
import 检测框模块
from find_class_module import find_class_and_perform_action
from yolo2.find_and_perform_action_module import find_and_perform_action
import image_search_module

from yolo2.yolo找图模块 import click_target_image
from yolo2.循环找图模块 import run_action_condition_found, run_action_condition_not_found
from 循环找图模块2 import run_action_until_image_disappears, simple_call
import subprocess
import sys
import 分任务模块
def main():
    if os.name == 'nt':
        import ctypes
        kernel32 = ctypes.windll.kernel32
        kernel32.SetConsoleMode(kernel32.GetStdHandle(-11), 7)
GREEN = '\033[92m'
# 定义重置颜色的 ANSI 转义序列
RESET = '\033[0m'
print(f"{GREEN}启动分地图2程序{RESET}")

if hasattr(检测框模块, 'MODULE_LOADED') and 检测框模块.MODULE_LOADED:
    print("检测框模块已正确加载")
else:
    print("检测框模块加载失败")

def draw_detection_boxes(screenshot, results):
    for result in results:
        boxes = result.boxes.cpu().numpy()
        for box in boxes:
            class_id = int(box.cls[0])
            x1, y1, x2, y2 = box.xyxy[0].astype(int)
            center_x = int((x1 + x2) / 2)
            center_y = int((y1 + y2) / 2)

            # 在图像上绘制检测框
            cv2.rectangle(screenshot, (x1, y1), (x2, y2), (0, 255, 0), 2)
            cv2.putText(screenshot, f"Class {class_id}", (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)

    return screenshot

# 禁用 pyautogui 的安全保护机制
pyautogui.FAILSAFE = False

# 加载 YOLOv8n 模型并指定使用GPU
model = YOLO(r"h:\yol\yolo\v11\runs\detect\train41\weights\best.pt").to('cuda')

hwnd = 检测框模块.find_window()
if hwnd is None:
    print("未找到标题中包含'梦幻西游'的窗口")
else:
    left, top, right, bottom = win32gui.GetWindowRect(hwnd)
    window_width = right - left
    window_height = bottom - top
#巡逻=============================

    def action_C():
        print("接到了巡逻任务")

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110,
               offset_y=50, 置信度=0.3, click_type='left')


        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=10, offset_x=-50, offset_y=100,
                   置信度=0.3, click_type='left')
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=10, offset_x=200, offset_y=-100,
                   置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 1, action_a)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')

        def action_c():
            print("秒过去")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=60, offset_y=0,
                   置信度=0.3, click_type='left')
            pyautogui.moveRel(-60, 0)
            screenshot = 检测框模块.get_window_screenshot(hwnd)
            save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
            if not os.path.exists(save_dir):
                os.makedirs(save_dir)
            save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
            cv2.imwrite(save_path, screenshot)
            print(f"游戏窗口截图已保存到 {save_path}")
            pyautogui.click()
            pyautogui.click()
            pyautogui.moveRel(60, 0)
            pyautogui.hotkey('alt', 'q')
            pyautogui.hotkey('alt', 'q')

        find_class_and_perform_action(hwnd, model, 151, action_c)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')

        def action_a():
            print("巡逻结束，接任务")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=10, offset_x=-10,
                   offset_y=-30, 置信度=0.3, click_type='left')

            pyautogui.click()

        find_class_and_perform_action(hwnd, model, 148, action_a)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

        # 开始分任务流程，启动目标脚本，暂停当前脚本。
        subprocess.run([sys.executable, "分地图2.py"], check=False)
        print("尝试启动分地图2")
        time.sleep(1)
        sys.exit()
    def action_D():  # 动作二：某图消失后执行的动作
        print("执行 D 动作")
        return True
    image_folder = r'h:\zdh\yolo2\xl'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

    # 巡逻=============================


    def action_C():
            print("发现强盗")
            def action_c():
                print("秒过去")
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=60, offset_y=0,
                       置信度=0.3, click_type='left')
                pyautogui.moveRel(-60, 0)
                screenshot = 检测框模块.get_window_screenshot(hwnd)
                save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
                if not os.path.exists(save_dir):
                    os.makedirs(save_dir)
                save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
                cv2.imwrite(save_path, screenshot)
                print(f"游戏窗口截图已保存到 {save_path}")
                pyautogui.click()
                pyautogui.click()
                pyautogui.moveRel(60, 0)
                pyautogui.hotkey('alt', 'q')
                pyautogui.hotkey('alt', 'q')

            find_class_and_perform_action(hwnd, model, 151, action_c)

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

            def action_a():
                print("巡逻结束，接任务")
                movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=10, offset_x=-10,
                       offset_y=-30, 置信度=0.3, click_type='left')
                pyautogui.click()
            find_class_and_perform_action(hwnd, model, 148, action_a)
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

            # 开始分任务流程，启动目标脚本，暂停当前脚本。
            subprocess.run([sys.executable, "分地图2.py"], check=False)
            print("尝试启动分地图2")
            time.sleep(1)
            sys.exit()
    def action_D():
     print("没看到强盗，找下一个")
     return True
    image_folder = r'h:\zdh\yolo2\cbpw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

    # 傲来国======================
    def action_C():# # 找到傲来国图就打开飞行符，否则执行动作4然后退出的功能
        def action_a():
            pyautogui.hotkey('alt', 'F7') #打开飞行符
        find_class_and_perform_action(hwnd, model, 15, action_a)  # 没看到傲来FF就重新打开飞行符

        def action():  # 找到就
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=15, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')  # 点击傲来FF

        find_and_perform_action(hwnd, model, 15, action)

        def action():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 5, action)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        分任务模块.assign_tasks()
        sys.exit()
    def action_D():
     print("没看到傲来国，退出循环，找下一个")
     return True
    image_folder = r'h:\zdh\yolo2\alg'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)
#西凉======================
    def action_C():  # 动作一：循环执行的动作
        def action_a():
            pyautogui.hotkey('alt', 'F7') #打开飞行符
        find_class_and_perform_action(hwnd, model, 34, action_a)  # 没看到西凉FF就重新打开飞行符

        def action():  # 找到就
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=34, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')  # 点击西凉FFid34

        find_and_perform_action(hwnd, model, 34, action)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
               置信度=0.3, click_type='left')
        分任务模块.assign_tasks()
        sys.exit()
    def action_D():  # 动作二：某图消失后执行的动作
        print("执行 D 动作")
        return True


    image_folder = r'h:\zdh\yolo2\xlng'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)
    # 朱紫国=====================
    def action1():
        print("找到目标，循环执行任务中")

        def action_a():
            pyautogui.hotkey('alt', 'F7')  # 打开飞行符

        find_class_and_perform_action(hwnd, model, 17, action_a)  # 没看到西凉FF就重新打开飞行符

        def action():  # 找到就朱紫国ff就点击
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=17, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')  # 点击西凉FFid34

        find_and_perform_action(hwnd, model, 17, action)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')

        分任务模块.assign_tasks()
        sys.exit()
    def action2():
        print("执行动作2")
        return True
    # 图片文件夹路径
    image_folder = r'H:\zdh\yolo2\zzg'
    # 调用封装好的函数
    simple_call(image_folder, action1, action2)
    # 长寿村======================
    def action_C():  # # 找到长寿村图就打开飞行符，否则执行动作4然后退出的功能
        print("找到长寿村")
        def action_a():
            pyautogui.hotkey('alt', 'F7')  # 打开飞行符
        find_class_and_perform_action(hwnd, model, 21, action_a)  # 没看到FF就重新打开飞行符

        def action():  # 找到csff就点击
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=21, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')  # 点击CSFFid34
        find_and_perform_action(hwnd, model, 21, action)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        分任务模块.assign_tasks()
        sys.exit()
        print("执行 C 动作")


    def action_D():
        print("没看到长寿村，退出循环，找下一个")
        return True

    image_folder = r'h:\zdh\yolo2\csc'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

 # 建邺城======================
    def action_C():  # # 找到建邺城图就打开飞行符，否则执行动作4然后退出的功能
        def action_a():
            pyautogui.hotkey('alt', 'F7')  # 打开飞行符
        find_class_and_perform_action(hwnd, model, 35, action_a)  # 没看到FF就重新打开飞行符

        def action():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=35, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 35, action)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,offset_y=-10, 置信度=0.3, click_type='left')  # 点掉对话框。
        分任务模块.assign_tasks()
        print("尝试启动分任务程序")
        sys.exit()
        print("执行 C 动作")

    def action_D():
        print("没看到建邺城，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\jyc'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)
 # 宝象国============================================================================
    def action_C():  # 找到宝象国图就打开飞行符，否则执行动作4然后退出的功能
        def action_a():
            pyautogui.hotkey('alt', 'F7')  # 打开飞行符
        find_class_and_perform_action(hwnd, model, 33, action_a)  # 没看到FF就重新打开飞行符


        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=33, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')  # 点击CSFFid34

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,offset_y=-10, 置信度=0.3, click_type='left')  # 点掉对话框。
        print("启动分任务模块")
        分任务模块.assign_tasks()


    def action_D():
        print("没看到宝象国，退出循环，找下一个")
        return True

    image_folder = r'h:\zdh\yolo2\bxg'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)
    # 藏经阁============================================================================
    def action_C():  # 找到藏经阁图就打开飞行符，否则执行动作4然后退出的功能
        print(f"{GREEN}飞去藏经阁{RESET}")
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-100, offset_y=50,
                   置信度=0.3, click_type='left')  #
        find_and_perform_action(hwnd, model, 5, action)
        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77
        find_class_and_perform_action(hwnd, model, 74, action_a)  # 75是国境点

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=74, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点击江南传送点
        find_and_perform_action(hwnd, model, 74, action)
        pyautogui.hotkey('alt', 'e')
        time.sleep(0.1)
        pyautogui.moveRel(30, -190)
        pyautogui.click()
        time.sleep(0.1)
        pyautogui.click()
        time.sleep(0.1)
        分任务模块.assign_tasks()
        sys.exit()
        print("执行 C 动作")

    def action_D():
        print("没看到藏经阁，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\cjg'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

    # 程咬金============================================================================
    def action_C():  # 找到程咬金就打开道具栏，否则执行动作4然后退出的功能
        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )

        find_class_and_perform_action(hwnd, model, 25, action_a)

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=73, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点击江南传送点
        find_and_perform_action(hwnd, model, 73, action)
        pyautogui.hotkey('alt', 'e')

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
                   置信度=0.3, click_type='left')  # 点掉对话框
        find_and_perform_action(hwnd, model, 5, action)

        pyautogui.moveRel(80, -80)
        pyautogui.click()

        分任务模块.assign_tasks()
        sys.exit()
        print("执行 C 动作")


    def action_D():
        print("没看程咬金，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\cyj'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

    # 江南野外============================================================================
    def action_C():  # 找到江南图就打开飞行符，否则执行动作4然后退出的功能
        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10,offset_y=-10, 置信度=0.3, click_type='right', )
        find_class_and_perform_action(hwnd, model, 25, action_a)


        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=25, offset_x=-10,offset_y=-10, 置信度=0.3, click_type='left')  # 点击江南传送点
        find_and_perform_action(hwnd, model, 25, action)
        pyautogui.hotkey('alt', 'e')
        #================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,offset_y=-10, 置信度=0.3, click_type='left')  # 点击江南传送点
        find_and_perform_action(hwnd, model, 5, action)

        pyautogui.moveRel(0, -80)
        pyautogui.click()


        分任务模块.assign_tasks()
        sys.exit()



    def action_D():
        print("没看江南图，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\jnyw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 普陀山============================================================================
    def action_C():  # 找到普陀山图就打开道具栏，否则执行动作D然后退出的功能
        print("执行去普陀动作")
        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10,
                   offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 75, action_a)  # 75是国境点
        pyautogui.hotkey('alt', 'e')

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=75, offset_x=-10,
                   offset_y=-10,
                   置信度=0.2, click_type='left')  # 点击江南传送点

        find_and_perform_action(hwnd, model, 75, action)  # 找到国境点就一直点

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10,
                   offset_y=-10,
                   置信度=0.3, click_type='left')  # 点击江南传送点

        find_and_perform_action(hwnd, model, 5, action)
        time.sleep(0.41)
        pyautogui.moveRel(-180, 150)
        pyautogui.click()
        time.sleep(0.5)
        pyautogui.click()
        time.sleep(0.5)

        def action_a():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left',
                   )

        find_class_and_perform_action(hwnd, model, 88, action_a)

        pyautogui.hotkey('F9')
        print(f"{GREEN}看到仙女了{RESET}")

        def action():
            print(f"{GREEN}点击传送{RESET}")

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-50,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 148, action)



        分任务模块.assign_tasks()
        sys.exit()
    def action_D():#这里就是没找到时执行的退出动作
        print("没看到普陀，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\pts'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 潮音洞============================================================================

    # 潮音洞============================================================================
    def action_C():  # 找到潮音洞图就打开道具栏，否则执行动作D然后退出的功能
        print("执行去潮音洞")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 75, action_a)  # 75是国境点
        pyautogui.hotkey('alt', 'e')
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=75, offset_x=-10, offset_y=-10,
                   置信度=0.2, click_type='left')  # 点击江南传送点

        find_and_perform_action(hwnd, model, 75, action)  # 找到国境点就一直点


        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点击江南传送点

        find_and_perform_action(hwnd, model, 5, action)
        time.sleep(0.41)
        pyautogui.moveRel(-180, 150)
        pyautogui.click()
        time.sleep(0.5)
        pyautogui.click()
        time.sleep(0.5)
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left',
                   )

        find_class_and_perform_action(hwnd, model, 88, action_a)

        pyautogui.hotkey('F9')
        print(f"{GREEN}看到仙女了{RESET}")

        def action():
            print(f"{GREEN}点击传送{RESET}")

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10, offset_y=-50,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 148, action)

        分任务模块.assign_tasks()
        sys.exit()


    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到潮音洞，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\cyd'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 凌波城============================================================================
    def action_C():  # 找到凌波城图就打开道具栏，否则执行动作D然后退出的功能
        print("去凌波城")
        print("点掉对话框")
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
               置信度=0.3, click_type='left')
        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77
        find_class_and_perform_action(hwnd, model, 78, action_a)  # 75是国境点
        print(f"{GREEN}打开旗子了{RESET}")

        def action_a():
            print(f"{GREEN}飞去驿站{RESET}")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=78, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点大唐驿站送点

            print("飞去长安")

        find_class_and_perform_action(hwnd, model, 86, action_a)
        print(f"{GREEN}到驿站了{RESET}")
        pyautogui.hotkey('alt', 'e')
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=86, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left', )
            print("点击驿站老板")
        find_class_and_perform_action(hwnd, model, 148, action_a)

        def action():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left', )
        find_and_perform_action(hwnd, model, 148, action)
        time.sleep(0.5)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')
        time.sleep(10)


        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 153, action_a)
        分任务模块.assign_tasks()
        sys.exit()

    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到凌波城，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\lbc'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 地藏王============================================================================
    def action_C():  # 找到凌波城图就打开道具栏，否则执行动作D然后退出的功能
        print("发现要给地藏王送信，去地府")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 18, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=18, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 78, action_a)  # 75是国境点

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=78, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点大唐驿站送点

        find_and_perform_action(hwnd, model, 78, action)  # 找到国境点就一直点

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
                   置信度=0.3, click_type='left')  # 点击江南传送点
        find_and_perform_action(hwnd, model, 5, action)
        pyautogui.hotkey('alt', 'e')
        print("关闭对话框")
        def action_a():
            print("点击驿站老板")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=86, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left', )
            print("点击成功")

        find_class_and_perform_action(hwnd, model, 148, action_a)
        def action():
            print("点选项一")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 148, action)

        # 没看到国境地图名称就继续点，直到进入国境。
        pyautogui.hotkey('tab')
        click_target_image(r"h:\zdh\yolo2\png\dfzb.png", offset_x=0, offset_y=0, click_type='left')
        pyautogui.hotkey('tab')
        print(f"{GREEN}等待路口出现{RESET}")
        def action_when_appears():
            print("找到了关键图片，到路口了，进去")
            click_target_image(r"h:\zdh\yolo2\png\dflk.png", offset_x=0, offset_y=0, click_type='left')
        # 定义当图片消失时要执行的动作
        def action_when_disappears():
            print("还没到路口，继续走")
            # 这里可以添加你需要执行的具体退出动作
        # 模板图片的路径
        template_path = r"h:\zdh\yolo2\png\dflk.png"
        image_search_module.loop_until_appears_and_loop_action(
            template_path=template_path,
            action_when_appears=action_when_appears,
            action_when_disappears=action_when_disappears,
            threshold=0.98
        )


        def action_a():
            print(f"{GREEN}点目标直到选项一出现{RESET}")

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left', )

        find_class_and_perform_action(hwnd, model, 148, action_a)

        def action_B():
            print("执行 B 动作")
            return True
        run_action_condition_not_found(hwnd, model, r'h:\zdh\yolo2\png\dfdt.png', action_A, action_B)
        pyautogui.hotkey('tab')
        click_target_image(r"h:\zdh\yolo2\png\dzwzb.png", offset_x=0, offset_y=0, click_type='left')
        pyautogui.hotkey('tab')

        def action_A():  # 调用找不到某图就循环执行动作1，否则执行动作2然后退出的功能
            print("去地藏王府的路上")
        def action_B():
            print("来到地藏王府了，开始分任务")
            分任务模块.assign_tasks()
            sys.exit()
            return True
        run_action_condition_not_found(hwnd, model, r'h:\zdh\yolo2\png\dzwfdt.png', action_A, action_B)

    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到地藏王，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\dzw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 东海龙王============================================================================
    def action_C():  # 找到凌波城图就打开道具栏，否则执行动作D然后退出的功能
        print("执行去龙宫动作")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 23, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=23, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 26, action_a)  # 26是东海湾点

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=26, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点大唐驿站送点

        find_and_perform_action(hwnd, model, 26, action)  # 找到东海湾点点就一直点

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
                   置信度=0.3, click_type='left')
        find_and_perform_action(hwnd, model, 5, action)

        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=80, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left',
               )

        def action():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
                   offset_y=-10, 置信度=0.3, click_type='left',
                   )
        find_and_perform_action(hwnd, model, 148, action)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=89, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left'
               )

        def action():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 148, action)

        def action_B():
            print("执行 B 动作")
            return True

        image_folder = r'h:\zdh\yolo2\cjg'
        run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

        分任务模块.assign_tasks()
        sys.exit()

    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到东海王，退出循环，找下一个")
        return True


    image_folder = r'h:\zdh\yolo2\dhlw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 东海湾============================================================================
    def action_C():  # 找到东海湾就打开道具栏，否则执行动作D然后退出的功能
        print("执行去东海湾动作")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 23, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=23, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            print("使用傲来旗")
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 26, action_a)  # 26是东海湾点

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=26, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  #
        print("来到东海湾驿站前了")
        find_and_perform_action(hwnd, model, 26, action)  # 找到东海湾点点就一直点
        pyautogui.hotkey('alt', 'e')  # 打开道具栏
        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
                   置信度=0.3, click_type='left')
            print("关掉对话框")
        find_and_perform_action(hwnd, model, 5, action)
        def action_a():
             movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=80, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )

        print("点击东海湾驿站老板了")
        find_class_and_perform_action(hwnd, model, 148, action_a)

        print(f"{GREEN}看到选项我要去了{RESET}")

        def action():

            print(f"{GREEN}点我要去{RESET}")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')
        find_and_perform_action(hwnd, model, 148, action)

        分任务模块.assign_tasks()
        print("启动分任务")

    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到东海湾，退出循环，找下一个")
        return True

    image_folder = r'h:\zdh\yolo2\dhw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

    # 花果山============================================================================
    def action_C():  # 找到凌波城图就打开道具栏，否则执行动作D然后退出的功能
        print("去花果山")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 23, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=23, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 77, action_a)  # 26是东海湾点

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=77, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点大唐驿站送点

        find_and_perform_action(hwnd, model, 77, action)  # 找到花果山点点就一直点

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 5, action)


        def action_A():  # 调用找不到某图就循环执行动作1，否则执行动作2然后退出的功能
            print("执行 A 动作")

            pyautogui.moveRel(100, 10)
            pyautogui.click()

        def action_B():
            print("执行 B 动作")
            return True

        run_action_condition_not_found(hwnd, model, r'h:\zdh\yolo2\png\hgsdt.png', action_A, action_B)
        分任务模块.assign_tasks()
        sys.exit()


    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到花果山，退出循环，找下一个")
        return True

    image_folder = r'h:\zdh\yolo2\hgs'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # 长寿郊外============================================================================
    def action_C():  # 找到凌波城图就打开道具栏，否则执行动作D然后退出的功能
        print("找到长寿郊外")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 24, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=24, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 67, action_a)  # 26是东海湾点


        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=67, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')  # 点大唐驿站送点

        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 5, action)
        pyautogui.hotkey('alt', 'e')
        time.sleep(1)
        pyautogui.moveRel(200, 80)
        pyautogui.click()
        分任务模块.assign_tasks()
        sys.exit()

    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到花果山，退出循环，找下一个")
        return True

    image_folder = r'h:\zdh\yolo2\csjw'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)

# 女儿村============================================================================
    def action_C():  #  找到孙婆婆图就打开道具栏，否则执行动作D然后退出的功能

        screenshot = 检测框模块.get_window_screenshot(hwnd)
        save_dir = r"h:\yol\ultralytics\datasets\mycp\images"
        if not os.path.exists(save_dir):
            os.makedirs(save_dir)
        save_path = os.path.join(save_dir, f"screenshot_{int(time.time())}.png")
        cv2.imwrite(save_path, screenshot)
        print(f"游戏窗口截图已保存到 {save_path}")

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-110, offset_y=50,
                   置信度=0.3, click_type='left')

        find_and_perform_action(hwnd, model, 5, action)
        print(f"{GREEN}找到孙婆婆，去女儿村{RESET}")

        def action_a():
            pyautogui.hotkey('alt', 'e')  # 打开道具栏

        find_class_and_perform_action(hwnd, model, 23, action_a)  # 没看到旗子就重新打开道具栏

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=23, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='right', )
            # 以上是点开77

        find_class_and_perform_action(hwnd, model, 76, action_a)  #

        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=76, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')  # 点女儿送点

        find_and_perform_action(hwnd, model, 76, action)  # 一直点
        pyautogui.hotkey('alt', 'e')  # 打开道具栏
        # ================点掉对话框


        def action_A():  # 调用找不到某图就循环执行动作1，否则执行动作2然后退出的功能
            print("执行 A 动作")

            pyautogui.moveRel(-100, 10)
            pyautogui.click()

        def action_B():
            print("执行 B 动作")
            return True

        run_action_condition_not_found(hwnd, model, r'h:\zdh\yolo2\png\necdt.png', action_A, action_B)

        def action_a():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=53, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 63, action_a)
        def action_a():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=53, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 70, action_a)
        def action_a():

            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=70, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 5, action_a)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        print("启动主程序")
        subprocess.run([sys.executable, "主程序.py"], check=False)
        sys.exit()
    def action_D():  # 这里就是没找到时执行的退出动作
        print("没看到孙婆婆，退出循环，找下一个")
        return True
    image_folder = r'h:\zdh\yolo2\spp'
    run_action_until_image_disappears(hwnd, image_folder, action_C, action_D)


    # =驯兽===========================================================================
    def action():  # 找到帮派召唤兽就
        print("找到驯兽任务，开始驯兽")
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
               置信度=0.3, click_type='left')
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=27, offset_x=50, offset_y=50,
               置信度=0.3, click_type='left', click_error=500)

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=27, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')

        find_class_and_perform_action(hwnd, model, 28, action_a)  # 点黑色机关人

        def action_a():
          movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=28, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 14, action_a)

        def action_a():
          pyautogui.click()
        find_class_and_perform_action(hwnd, model, 20, action_a)

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=20, offset_x=20,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 36, action_a)
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=36, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 148, action_a)
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='', )
        find_class_and_perform_action(hwnd, model, 1, action_a)

        def action_c():
            print("秒过去")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=1, offset_x=0, offset_y=0,
                   置信度=0.3, click_type='right')

            pyautogui.click()
            pyautogui.click()


        find_class_and_perform_action(hwnd, model, 151, action_c)
        # ================点掉对话框
        def action():  # 定义动作,找到类就做某事
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=5, offset_x=-10, offset_y=-10,
                   置信度=0.3, click_type='left')
        find_and_perform_action(hwnd, model, 5, action)

        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=36, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
        find_class_and_perform_action(hwnd, model, 28, action_a)
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=28, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left', )
            pyautogui.click()

        find_class_and_perform_action(hwnd, model, 14, action_a)

        # 封装的检查和执行动作逻辑，没找到则
        def action_a():
            pyautogui.moveRel(40, 50)
            pyautogui.click()
        find_class_and_perform_action(hwnd, model, 13, action_a)

        # 封装的检查和执行动作逻辑，没找到则
        def action_a():
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=13, offset_x=-20,
                   offset_y=-30, 置信度=0.3, click_type='left')
            pyautogui.click()

        find_class_and_perform_action(hwnd, model, 27, action_a)

        def action_a():
            print("驯兽结束，接新任务")
            movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=10, offset_x=-10,
                   offset_y=-30, 置信度=0.3, click_type='left')
            pyautogui.click()

        find_class_and_perform_action(hwnd, model, 148, action_a)
        movefb(hwnd, left, top, window_width, window_height, model, 要点击的目标类=148, offset_x=-10,
               offset_y=-10, 置信度=0.3, click_type='left')

        # 开始分任务流程，启动目标脚本，暂停当前脚本。
        subprocess.run([sys.executable, "分地图2.py"], check=False)
        print("尝试启动分地图2")
        sys.exit()
    find_and_perform_action(hwnd, model, 62, action)#找到驯兽面板就
    print("什么都没找到，结束程序")
    sys.exit()


cv2.destroyAllWindows()
if __name__ == "__main__":
    main()